﻿using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MTGenome.Gatherer;
using MTGenome.MTG;

namespace MTGenome.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (Database database = new Database(new DatabaseConnection("C:\\MTGenome.sdf")))
                {
                    GathererServer gatherer = new GathererServer();

                    Console.WriteLine("Do you want to run the 'hole finder'?");
                    if (Console.ReadLine().ToLower().Contains("y"))
                    {
                        object locker = new object();

                        var idQuery = database.Cards.Select(c => c.MultiverseID);
                        var query = database.Cards.Where(c =>
                                !idQuery.Contains(c.MultiverseID + 1))
                            .Select(c => c.MultiverseID + 1);

                        Parallel.ForEach(query.ToArray(), id =>
                        {
                            for (int i = id; ; i++)
                            {
                                if (i != id)
                                {
                                    lock (locker)
                                    {
                                        if (idQuery.Contains(i))
                                        {
                                            break;
                                        }
                                    }
                                }

                                ICard card = gatherer.GetCard(i);

                                if (!string.IsNullOrEmpty(card.Name))
                                {
                                    try
                                    {
                                        lock (locker)
                                        {
                                            database.InsertOnSubmit(card);
                                            database.SubmitChanges();
                                        }

                                        Console.WriteLine(card);
                                    }
                                    catch
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        });

                        throw new Exception("Done updating database!");
                    }

                    Console.WriteLine("Do you want to just update the database?");
                    if (Console.ReadLine().ToLower().Contains("y"))
                    {
                        object locker = new object();

                        Parallel.ForEach(database.Cards.Where(c => c.Power != null).ToArray(), card =>
                        {
                            bool changed = card.UpdateCard(gatherer.GetCard(card.MultiverseID));

                            lock (locker)
                            {
                                if (changed)
                                {
                                    database.SubmitChanges();
                                }
                            }

                            Console.WriteLine(card);
                        });

                        throw new Exception("Done updating database!");
                    }

                    Console.WriteLine("WARNING! About to wipe DB. You sure you want to continue?");
                    if (Console.ReadLine().ToLower().Contains("y"))
                    {
                        //delete database, should cascade
                        database.ExecuteCommand("DELETE FROM Expansions");
                    }
                    else
                    {
                        Console.WriteLine("Skipping DB wipe...");
                    }


                    Console.WriteLine("Do you want to run the ForEach stategy?");

                    bool forEach = Console.ReadLine().ToLower().StartsWith("y");

                    object @lock = new object();
                    gatherer.Enumerated += (sender, e) =>
                    {
                        ICard card = e.Value;
                        if (card.Text.Contains("<"))
                        {
                            //Look here! there is html in the card's text
                            System.Diagnostics.Debugger.Break();
                        }

                        lock (@lock)
                        {
                            try
                            {
                                if (!database.Cards.Select(c => c.MultiverseID).Contains(card.MultiverseID))
                                {
                                    Console.WriteLine(card);

                                    if (!database.Expansions.Select(ex => ex.ID).Contains(card.Expansion))
                                    {
                                        database.InsertOnSubmit(new Expansion
                                        {
                                            ID = card.Expansion,
                                            Name = card.ExpansionName,
                                        });
                                    }

                                    database.InsertOnSubmit(card);
                                    database.SubmitChanges();
                                }
                            }
                            catch (Exception exc)
                            {
                                exc.ToString();
                            }
                        }
                    };

                    if (forEach)
                    {
                        Console.WriteLine("Reverse?");

                        gatherer.ForEachGatherer(Console.ReadLine().ToLower().StartsWith("y"));
                    }
                    else
                    {
                        Console.WriteLine("Enter the start id (Defaults to 0):");

                        int x = 0;
                        int.TryParse(Console.ReadLine(), out x);

                        gatherer.ForGatherer(x, int.MaxValue);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
            }

            Console.WriteLine("DONE!");
            Console.ReadLine();
        }
    }
}
